package com.hy.erp.inventory.service ;

import java.util.ArrayList ;
import java.util.List ;

import org.springframework.stereotype.Component;

/*import javax.ejb.Stateless ;

import org.jboss.seam.ScopeType ;
import org.jboss.seam.annotations.Name ;
import org.jboss.seam.annotations.Scope ;
*/
import com.hy.enterprise.architecture.foundation.ArchitectureFoundationException;
import com.hy.enterprise.framework.service.business.AbstractBusinessService;
import com.hy.erp.inventory.dao.PersonDao;
import com.hy.erp.inventory.dao.interfaces.IPersonDao;
import com.hy.erp.inventory.pojo.Contact;
import com.hy.erp.inventory.pojo.Customer;
import com.hy.erp.inventory.pojo.Education;
import com.hy.erp.inventory.pojo.Family;
import com.hy.erp.inventory.pojo.Health;
import com.hy.erp.inventory.pojo.Person;
import com.hy.erp.inventory.pojo.RewardsPunishment;
import com.hy.erp.inventory.pojo.SkillLevel;
import com.hy.erp.inventory.pojo.Supplier;
import com.hy.erp.inventory.pojo.Training;
import com.hy.erp.inventory.pojo.Working;
import com.hy.erp.inventory.pojo.interfaces.IContact;
import com.hy.erp.inventory.pojo.interfaces.IEducation;
import com.hy.erp.inventory.pojo.interfaces.IFamily;
import com.hy.erp.inventory.pojo.interfaces.IHealth;
import com.hy.erp.inventory.pojo.interfaces.IPerson;
import com.hy.erp.inventory.pojo.interfaces.IRewardsPunishment;
import com.hy.erp.inventory.pojo.interfaces.ISkillLevel;
import com.hy.erp.inventory.pojo.interfaces.ITraining;
import com.hy.erp.inventory.pojo.interfaces.IWorking;
import com.hy.erp.inventory.service.interfaces.IPersonService;
//import com.hy.erp.inventory.vsoft.libra.enterprise.framework.persistence.entity.AbstractPersistentEntity;
//import com.hy.erp.inventory.vsoft.libra.enterprise.framework.util.lang.UUIDUtil;
/**
 * 
 * <ul>
 * <li>开发作者：李冰</li>
 * <li>设计日期：2010-10-13；时间：上午11:26:58</li>
 * <li>类型名称：PersonService</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
/*@ Name ( "personService" )
@ Stateless
@ Scope ( ScopeType.STATELESS )*/
//@Component("personService")
public class PersonService
							extends
							AbstractBusinessService < PersonDao >
																implements
																IPersonService
{

	@ Override
	public String addPerson (
								Person person )
	{
		if ( null == person )
		{
			return null ;
		}
		else
		{
			IPersonDao persondao =
									( IPersonDao ) this
														.getDao ( IPerson.class ) ;
			return persondao
							.persist1 ( person );
		}
	}

	@ Override
	public boolean modifyPerson (
									IPerson person )
	{
		if((null==person )||
				(null==person.getIdentifier ( ))||
				(0==person.getIdentifier ( ).trim ( ).length ( )))
			
		{
			return true;
		}
		else
		{
			IPersonDao personDao =( IPersonDao ) this.getDao ( IPerson.class );
			if ( null == person )
			{
				throw new ArchitectureFoundationException  (
				                                            "修改实体数据时发生错误，所需要修改的实体在数据库中并不存在" ) ;
			}
			return personDao.merge ( person )!=null;
		}
	}

	@ Override
	public Integer removePerson (
									String [ ] personIds )
	{
		if((null==personIds )||(0==personIds.length))
		{
			return new Integer (0);
		}
		else{
			IPersonDao personDao =( IPersonDao ) this.getDao ( IPerson.class );
			return personDao.remove ( personIds );
		}
	}

	@ Override
	public List < IPerson > getPersonById (
											String [ ] Id )
	{
		if((Id.length!=0)||(Id!=null))
		{
			IPersonDao personDao =( IPersonDao ) this.getDao ( IPerson.class );
			personDao.findByIdentifier ( Id );
		}
		return null ;
	}

	@ Override
	public String [ ] addPerson (
									Object [ ] objects )
	{
		if(objects==null)
		{
			return null;
		}
		else
		{

			IPersonDao personDao =( IPersonDao ) this.getDao ( IPerson.class );
			Person[] persons=new Person[objects.length];
			for ( int i =
							0 ; i < objects.length ; i ++ )
			{
				Person person=( Person ) objects[i];
				List < IEducation> educationd=new ArrayList < IEducation > ( );	
				List < IFamily> familys=new ArrayList < IFamily > ( );	
				List < IHealth> healths=new ArrayList < IHealth > ( );	
				List < ITraining> trainings=new ArrayList < ITraining > ( );	
				List < IWorking> workings=new ArrayList < IWorking > ( );	
				List < IRewardsPunishment> rewardsPunishments=new ArrayList < IRewardsPunishment > ( );	
				List < ISkillLevel> skillLevels=new ArrayList < ISkillLevel > ( );	
				
				List < Customer> customers=new ArrayList < Customer > ( );	
				List < Supplier> suppliers=new ArrayList < Supplier > ( );	
				List < IContact> contacts=new ArrayList < IContact > ( );	
				if(person.getEducation ( )!=null)
				{
					Education ed =
						null;
					educationd=person.getEducation ( );
					System.out
								.println (educationd.size ( )+"nnnnnnnnnnnnnnnnnnnnnnn") ;
					for(int j=0;j<educationd.size ( );j++)
					{
						ed=new Education();
				        String	uuid = UUIDUtil
			        .randomUUID ( );
				 ed=	( Education ) educationd.get ( j );
				ed.setIdentifier ( uuid );
				}
					person.setEducation ( educationd );
				}
				
				if(person.getFamily  ( )!=null)
				{
					Family ed =
						null;
					familys=person.getFamily( );
					
					for(int j=0;j<familys.size ( );j++)
					{
						ed=new Family();
				        String	uuid = UUIDUtil
			        .randomUUID ( );
				 ed=	( Family ) familys.get ( j );
				ed.setIdentifier ( uuid );
				}
					person.setFamily ( familys );
				}
			
				
				if(person.getHealth   ( )!=null)
				{
					Health ed =
						null;
					healths=person.getHealth ( );
					
					for(int j=0;j<healths.size ( );j++)
					{
						ed=new Health();
				        String	uuid = UUIDUtil
			        .randomUUID ( );
				 ed=	( Health ) healths.get ( j );
				ed.setIdentifier ( uuid );
				}
					person.setHealth ( healths );
				}
			
				if(person.getTraining ( )!=null)
				{
					Training ed =
						null;
					trainings=person.getTraining ( );
					
					for(int j=0;j<trainings.size ( );j++)
					{
						ed=new Training();
				        String	uuid = UUIDUtil
			        .randomUUID ( );
				 ed=	( Training ) trainings.get ( j );
				ed.setIdentifier ( uuid );
				}
					person.setTraining  ( trainings );
				}
			
				if(person.getWorking  ( )!=null)
				{
					Working ed =
						null;
					workings=person.getWorking ( );
					
					for(int j=0;j<workings.size ( );j++)
					{
						ed=new Working();
				        String	uuid = UUIDUtil
			        .randomUUID ( );
				 ed=	( Working ) workings.get ( j );
				ed.setIdentifier ( uuid );
				}
					person.setWorking   ( workings );
				}
				
				
				if(person.getRewardsPunishment   ( )!=null)
				{
					RewardsPunishment ed =
						null;
					rewardsPunishments=person.getRewardsPunishment ( );
					
					for(int j=0;j<rewardsPunishments.size ( );j++)
					{
						ed=new RewardsPunishment();
				        String	uuid = UUIDUtil
			        .randomUUID ( );
				 ed=	( RewardsPunishment ) rewardsPunishments.get ( j );
				ed.setIdentifier ( uuid );
				}
					person.setRewardsPunishment   ( rewardsPunishments );
				}
				
				if(person.getSkillLevel   ( )!=null)
				{
					SkillLevel ed =
						null;
					skillLevels=person.getSkillLevel  ( );
					
					for(int j=0;j<skillLevels.size ( );j++)
					{
						ed=new SkillLevel();
				        String	uuid = UUIDUtil
			        .randomUUID ( );
				 ed=	( SkillLevel ) skillLevels.get ( j );
				ed.setIdentifier ( uuid );
				}
					person.setSkillLevel   ( skillLevels );
				}
				
				if(person.getCustomer    ( )!=null)
				{
					Customer ed =
						null;
					customers=person.getCustomer ( );
					
					for(int j=0;j<customers.size ( );j++)
					{
						ed=new Customer();
				        String	uuid = UUIDUtil
			        .randomUUID ( );
				 ed=	customers.get ( j );
				ed.setIdentifier ( uuid );
				}
					person.setCustomer  ( customers );
				}
				
				
				if(person.getSupplier    ( )!=null)
				{
					Supplier ed =
						null;
					suppliers=person.getSupplier ( );
					
					for(int j=0;j<suppliers.size ( );j++)
					{
						ed=new Supplier();
				        String	uuid = UUIDUtil
			        .randomUUID ( );
				 ed=	suppliers.get ( j );
				ed.setIdentifier ( uuid );
				}
					person.setSupplier   ( suppliers );
				}
				
				if(person.getContact   ( )!=null)
				{
					Contact ed =
						null;
					contacts=person.getContact( );
					
					for(int j=0;j<contacts.size ( );j++)
					{
						ed=new Contact();
				        String	uuid = UUIDUtil
			        .randomUUID ( );
				 ed=	( Contact ) contacts.get ( j );
				ed.setIdentifier ( uuid );
				}
					person.setContact    ( contacts );
				}
			
				persons[i]=person;
			}
			return personDao.persist ( persons ) ;
		}
	}

	@ Override
	public List < IPerson > getAllPerson ( )
	{
		IPersonDao personDao =( IPersonDao ) this.getDao ( IPerson.class );
		return personDao.find ( ) ;
	}

	@ Override
	public Person getPersonById (
									String id )
	{
		IPersonDao personDao =( IPersonDao ) this.getDao ( IPerson.class );
		return ( Person ) personDao.findByIdentifier ( id ) ;
	}

	@ Override
	public Person getPersonById (
									AbstractPersistentEntity id )
	{
		IPersonDao personDao =( IPersonDao ) this.getDao ( IPerson.class );
		String Id=id.get ( "identifier" );
		return ( Person ) personDao.findByIdentifier ( Id ) ;
	}

	@ Override
	public String [ ] modifyPerson (
										Object [ ] objects )
	{
		if (  null == objects ) 
		{
			return null;
		}
		else
		{
			IPersonDao personDao =( IPersonDao ) this.getDao ( IPerson.class );
			Person[] types=new Person[objects.length];
            for(int i=0;i<objects.length;i++)
            {
                  Person person=( Person ) objects[i];
            	
     			List < IEducation> educationd=new ArrayList < IEducation > ( );	
     			List < IFamily> familys=new ArrayList < IFamily > ( );	
				List < IHealth> healths=new ArrayList < IHealth > ( );	
				List < ITraining> trainings=new ArrayList < ITraining > ( );	
				List < IWorking> workings=new ArrayList < IWorking > ( );	
				List < IRewardsPunishment> rewardsPunishments=new ArrayList < IRewardsPunishment > ( );	
				List < ISkillLevel> skillLevels=new ArrayList < ISkillLevel > ( );	
				
				List < Customer> customers=new ArrayList < Customer > ( );	
				List < Supplier> suppliers=new ArrayList < Supplier > ( );	
				List < IContact> contacts=new ArrayList < IContact > ( );	
     			if(person.getEducation ( )!=null)
     			{
     				Education ed =
     					null;
     				educationd=person.getEducation ( );
     				
     				for(int j=0;j<educationd.size ( );j++)
     				{
     					ed=new Education();
     			       
     			 ed=( Education ) educationd.get ( j );
     			 if(ed.getIdentifier ( )==null)
     			 {
     			
     			 String	uuid = UUIDUtil
     		        .randomUUID ( );
     			ed.setIdentifier ( uuid );
     			
     			}	
     			 }
     				person.setEducation ( educationd );
            	
     			}
     			
     			
     			if(person.getFamily  ( )!=null)
				{
					Family ed =
						null;
					familys=person.getFamily( );
					
					for(int j=0;j<familys.size ( );j++)
					{
						ed=new Family();
				 ed=	( Family ) familys.get ( j );
				 if(ed.getIdentifier ( )==null)
     			 {
     			 String	uuid = UUIDUtil
     		        .randomUUID ( );
     			ed.setIdentifier ( uuid );
     			
     			}	
				}
					person.setFamily ( familys );
				}
     			
     			
     			

				if(person.getHealth   ( )!=null)
				{
					Health ed =
						null;
					healths=person.getHealth ( );
					
					for(int j=0;j<healths.size ( );j++)
					{
						ed=new Health();
				 ed=	( Health ) healths.get ( j );
				 if(ed.getIdentifier ( )==null)
     			 {
     			 String	uuid = UUIDUtil
     		        .randomUUID ( );
     			ed.setIdentifier ( uuid );
     			
     			}	
				}
					person.setHealth ( healths );
				}
			
				if(person.getTraining ( )!=null)
				{
					Training ed =
						null;
					trainings=person.getTraining ( );
					
					for(int j=0;j<trainings.size ( );j++)
					{
						ed=new Training();
				     
				 ed=	( Training ) trainings.get ( j );
				 if(ed.getIdentifier ( )==null)
     			 {
     			 String	uuid = UUIDUtil
     		        .randomUUID ( );
     			ed.setIdentifier ( uuid );
     			
     			}	
				}
					person.setTraining  ( trainings );
				}
			
				if(person.getWorking  ( )!=null)
				{
					Working ed =
						null;
					workings=person.getWorking ( );
					
					for(int j=0;j<workings.size ( );j++)
					{
						ed=new Working();
				 ed=	( Working ) workings.get ( j );
				 if(ed.getIdentifier ( )==null)
     			 {
     			 String	uuid = UUIDUtil
     		        .randomUUID ( );
     			ed.setIdentifier ( uuid );
     			
     			}	
				}
					person.setWorking   ( workings );
				}
				
				
				if(person.getRewardsPunishment   ( )!=null)
				{
					RewardsPunishment ed =
						null;
					rewardsPunishments=person.getRewardsPunishment ( );
					
					for(int j=0;j<rewardsPunishments.size ( );j++)
					{
						ed=new RewardsPunishment();
				 ed=	( RewardsPunishment ) rewardsPunishments.get ( j );
				 if(ed.getIdentifier ( )==null)
     			 {
     			 String	uuid = UUIDUtil
     		        .randomUUID ( );
     			ed.setIdentifier ( uuid );
     			
     			}	
				}
					person.setRewardsPunishment   ( rewardsPunishments );
				}
				
				if(person.getSkillLevel   ( )!=null)
				{
					SkillLevel ed =
						null;
					skillLevels=person.getSkillLevel  ( );
					
					for(int j=0;j<skillLevels.size ( );j++)
					{
						ed=new SkillLevel();
				 ed=	( SkillLevel ) skillLevels.get ( j );
				 if(ed.getIdentifier ( )==null)
     			 {
     			 String	uuid = UUIDUtil
     		        .randomUUID ( );
     			ed.setIdentifier ( uuid );
     			
     			}	
				}
					person.setSkillLevel   ( skillLevels );
				}
				
				if(person.getCustomer    ( )!=null)
				{
					Customer ed =
						null;
					customers=person.getCustomer ( );
					
					for(int j=0;j<customers.size ( );j++)
					{
						ed=new Customer();
				    
				 ed=	customers.get ( j );
				 if(ed.getIdentifier ( )==null)
     			 {
     			 String	uuid = UUIDUtil
     		        .randomUUID ( );
     			ed.setIdentifier ( uuid );
     			
     			}	
				}
					person.setCustomer  ( customers );
				}
				
				
				if(person.getSupplier    ( )!=null)
				{
					Supplier ed =
						null;
					suppliers=person.getSupplier ( );
					
					for(int j=0;j<suppliers.size ( );j++)
					{
						ed=new Supplier();
				 ed=	suppliers.get ( j );
				 if(ed.getIdentifier ( )==null)
     			 {
     			 String	uuid = UUIDUtil
     		        .randomUUID ( );
     			ed.setIdentifier ( uuid );
     			
     			}	
				}
					person.setSupplier   ( suppliers );
				}
				
				if(person.getContact   ( )!=null)
				{
					Contact ed =
						null;
					contacts=person.getContact( );
					
					for(int j=0;j<contacts.size ( );j++)
					{
						ed=new Contact();
				 ed=	( Contact ) contacts.get ( j );
				 if(ed.getIdentifier ( )==null)
     			 {
     			 String	uuid = UUIDUtil
     		        .randomUUID ( );
     			ed.setIdentifier ( uuid );
     			
     			}	
				}
					person.setContact    ( contacts );
				}
     			
     			
     			
     			
     			types[i]=person;
            }
			
			return personDao.merge ( types );
		}
	}

}
